var pro = document.getElementById('prouploadfile');
var resultfile="";
var setsize=0;
var filecount=0;
var filesize=0;
$('#upload_video').change(function(e){

    resultfile=document.getElementById('upload_video').files[0];

    //判断文件大小
    var num=1048576;
    var cha=Math.ceil(resultfile.size)/num;
    if(cha>200)
    {
        //alert(Math.ceil(resultfile.size/num));

        alert('请选取文件在200M以下的视频');
        return false;
    }
    var video_type="video/mp4";
    if(resultfile.type != video_type){
        alert("请选择格式为MP4的");
        return false;
    }

    //切片计算
    filesize= resultfile.size;
    setsize=1*1024*1024;
    filecount = filesize/setsize;
    //console.log(filecount)
    //定义进度条

    pro.max=parseInt(Math.ceil(filecount));

    clearCookie(resultfile.name);
    i =getCookie(resultfile.name);
    i = (i!=null && i!="")?parseInt(i):0;

    if(Math.floor(filecount)<i){
      //  alert("上传完成");
        pro.value=i+1;
        layui.use('element', function(){
            var element = layui.element;
            element.progress('progress', '100%');
        });

    }else{

     //   alert('视频1可以上传');
        pro.value=i;
        p=parseInt(i)*100/Math.ceil(filecount)

        layui.use('element', function(){
            var element = layui.element;

            element.progress('progress', p+'%');
        });
    }
})

//3.ajax上传

var stop=1;
function xhr2(){
    if(stop==1){
        return false;
    }
    if(resultfile==""){
        alert("请选择文件")
        return false;
    }
    i=getCookie(resultfile.name);
    i = (i!=null && i!="")?parseInt(i):0

    if(Math.floor(filecount)<parseInt(i)){
     //   alert("视频1上传完成");
        return false;
    }else{
        //alert(i)
    }
    var xhr = new XMLHttpRequest();//第一步
    //console.log(xhr);
    //新建一个FormData对象
    var formData = new FormData(); //++++++++++
    //追加文件数据

    //改变进度条
    pro.value=i+1;
    p=parseInt(i+1)*100/Math.ceil(filecount)

    layui.use('element', function(){
        var element = layui.element;
        element.progress('progress',parseInt(p)+'%');
    });
    //进度条
    //console.log(resultfile);
    if((filesize-i*setsize)>setsize){
        blobfile= resultfile.slice(i*setsize,(i+1)*setsize);
    }else{
        //最后分片
        blobfile= resultfile.slice(i*setsize,filesize);
        formData.append('lastone', Math.floor(filecount));
        $("#video_tip").html("进入视频处理阶段，请耐心等待……");
    }
    //console.log(videoid.value);
    //formData.append('videoid', videoid.value);
    formData.append('file', blobfile);
    //formData.append('goods_id',  goods_id.value);
    //return false;
    formData.append('blobname', i); //++++++++++
    formData.append('filename', resultfile.name); //++++++++++
    //post方式
    xhr.open('POST', 'modules/upload.php?act=upload'); //第二步骤
    //发送请求

    $('#UploadAction').html("暂停")
    xhr.send(formData);  //第三步骤
    $('#stop').val("暂停")
    //ajax返回
    xhr.onreadystatechange = function(){ //第四步

        if ( xhr.readyState == 4 && xhr.status == 200 ) {
            //console.log( xhr.responseText );
            if(xhr.responseText=="none"){
                alert("视频格式不支持");

                layui.use('element', function(){
                    var element = layui.element;

                    element.progress('progress','0%');
                });
                pro.value=0;
                return false;
            }else if(xhr.responseText==1){
                alert("上传视频失败！参数错误");

                layui.use('element', function(){
                    var element = layui.element;

                    element.progress('progress','0%');
                });
                //document.getElementById("drop_area").innerHTML="支持视频格式为'.flv' ,'.rmvb' , '.mp4'";
                pro.value=0;
                return false;
            }else if(i<filecount){
                xhr2();
            }else{
                //alert(xhr.responseText+"上传完成！");

                data_res = xhr.responseText
                data_res = data_res.replace(/[\r\n]/g,"");
                data_res = data_res.replace(/\ +/g,"");
                $('.video_path').val(data_res)

                $('#video').attr('src',filePath_+ data_res)
                console.log(filePath_ + data_res)
                if(data_res){
                    setTimeout(function(){
                        get_viedo_img();
                        $('#UploadAction').html("上传完成")
                    videoUploadSwitch_ = false;
                        clearCookie(resultfile.name);
                    }, 2000);
                }
            }
        }

    };
    //设置超时时间
    //xhr.timeout = 20000;
    xhr.ontimeout = function(event){
        alert('请求超时，网络拥堵！低于25K/s');
    }
    //captureImage($("#video").get(0));
    i=i+1;
    setCookie(resultfile.name,i,365)
}

//设置cookie
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/")
}
//清除cookie
function clearCookie(name) {
    setCookie(name, "", -1);
}
//获取cookie
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end))
        }
    }
    return ""
}


$('#UploadAction').click(function(){

    if(stop==1){
        if(videoUploadSwitch_){
            layui.use('layer', function(){
                var layer = layui.layer;
                layer.msg('有一个视频上传中');
            });
            return false;
        }
        videoUploadSwitch_ = true;

        stop = 0;
        xhr2();

    }else{
        stop = 1
        $('#UploadAction').html("继续")
        videoUploadSwitch_ = false;
    }
})






function get_viedo_img() {


    time = document.getElementById("video").duration;

    $('#duration').val(time)
    captureImage()
}






function captureImage() {

    var scale = 0.8;
    var canvas = document.createElement("canvas");

    video = document.getElementById("video");
    canvas.width = video.videoWidth * scale;
    canvas.height = video.videoHeight * scale;
    canvas.getContext('2d').drawImage(video, 0, 0, canvas.width,
        canvas.height);

    img_src = canvas.toDataURL("image/png");

    $('#video_img').val(img_src)
};



